// This is the main DLL file.

#include "stdafx.h"

#include "kkLogger.h"
#include "KillerKitty.h"

#include "kkUtils.h"
#include "kkRegExEngine.h"
#include "kkSignatureParser.h"
#include "kkDetectionEngine.h"
//for JNI
#include "kkJTest.h"

using namespace std;

KillerKitty::kkDetectionEngine *detectionEngine=NULL;

//Init the detection engine
bool __declspec(dllexport) initialize() {

	if(detectionEngine) delete detectionEngine;

	detectionEngine = new KillerKitty::kkDetectionEngine();

	return detectionEngine->initEngine();

}

string __declspec(dllexport) isMalicious(char *fname) {

	if(!detectionEngine) {
		KillerKitty::log.write(NULL,"kkDetectionEngine NOT initialized! Aborting detection...",KillerKitty::error);
		return "-1";
	}

	KillerKitty::kkDetectionEngine &d = *detectionEngine;
	
	//KillerKitty::log.write(NULL,"In KillerKitty",KillerKitty::debug);

	return d.isMalicious(fname);
}


//clean up and leave
void __declspec(dllexport) destroy() {

	delete detectionEngine;
	detectionEngine = NULL;

}


//java test interface

JNIEXPORT jstring JNICALL Java_kkWindows_kkScanWindow_isMaliciousJ (JNIEnv *env, jobject,jstring path) {

	char * myPath=(char *)env->GetStringUTFChars(path, 0);

	cout<<"From DLL -> "<<myPath;
	string virusName = isMalicious(myPath);
	
	
	jstring javaStr=env->NewStringUTF(virusName.c_str());
	
	return javaStr;

}

JNIEXPORT jstring JNICALL Java_kkWindows_kkScanWindow_initializeJ (JNIEnv *env, jobject){

	bool initTest=initialize();
	
	jstring ok=env->NewStringUTF("ok");
	jstring error=env->NewStringUTF("error");

	if(initTest)
		return ok;
	else
		return error;
}

JNIEXPORT void JNICALL Java_kkWindows_kkScanWindow_destroyJ (JNIEnv *env, jobject){

	destroy();


}
